package com.tsystems.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import com.tsystems.entities.Train;



public class TrainDAO extends AbstractDAO<Train> {

	public Train getTrainByNumber(String number) {
		
		EntityManager em = TrainsEntityManager.getInstance();	
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Train> criteria = builder.createQuery(Train.class);
		
		Root<Train> trains = criteria.from(Train.class);
		criteria.select(trains);
		criteria.where(builder.equal(trains.get("number"), number));
		List<Train> result = em.createQuery(criteria).getResultList();
		
		return !result.isEmpty() ? result.get(0) : null;
	}
	
	public List<Train> getTrainList() {
		
		EntityManager em = TrainsEntityManager.getInstance();	
		CriteriaBuilder builder = em.getCriteriaBuilder();
		CriteriaQuery<Train> criteria = builder.createQuery(Train.class);
		
		Root<Train> trains = criteria.from(Train.class);
		criteria.select(trains);
		
		List<Train> result = em.createQuery(criteria).getResultList();
		
		return result;
	}
	
	
	
}
